package com.code4hire.desktop.converter;

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

public class FtpUpload {

    public static void main(String[] args) {
        FTPClient client = new FTPClient();

        try {
            client.connect("ftp.drivehq.com");

            //
            // When login success the login method returns true.
            //

            boolean login = client.login("milandinic83", "Altera11");

            if (login) {
                System.out.println("Login success...");

                int replay = client.getReplyCode();

                if (FTPReply.isPositiveCompletion(replay)) {
                    // File file = new
                    // File("C:\\Users\\e.behravesh\\Pictures\\me2_rect.jpg");
                    // FileInputStream input = new FileInputStream(file);
                    // client.setFileType(FTP.BINARY_FILE_TYPE);
                    //
                    // if (!client.storeFile(file.getName(), input)) {
                    // System.out.println("upload failed!");
                    // }

                    // input.close();

                    try {
                        InputStream stO = new BufferedInputStream(client.retrieveFileStream("/home/mile/hb.jar"),
                                client.getBufferSize());

                        OutputStream stD = new FileOutputStream("hb.jar");

                        org.apache.commons.net.io.Util.copyStream(stO, stD, client.getBufferSize(),
                        /*
                         * I'm using the UNKNOWN_STREAM_SIZE constant here, but
                         * you can use the size of file too
                         */
                        org.apache.commons.net.io.CopyStreamEvent.UNKNOWN_STREAM_SIZE,
                                new org.apache.commons.net.io.CopyStreamAdapter() {
                                    public void bytesTransferred(long totalBytesTransferred, int bytesTransferred,
                                            long streamSize) {
                                        // Your progress Control code here
                                        System.out.println("bytesTransferred " + bytesTransferred
                                                + " totalBytesTransferred " + totalBytesTransferred);
                                    }
                                });
                        client.completePendingCommand();
                    } catch (Exception e) {

                    }

                }
                //
                // When logout success the logout method returns true.
                //
                boolean logout = client.logout();
                if (logout) {
                    System.out.println("Logout from FTP server...");
                }
            } else {
                System.out.println("Login fail...");
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                //
                // Closes the connection to the FTP server
                //
                client.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
